﻿using System;
using System.Collections.Generic;
using Appology.SugarSync.IO.Primitives;
using System.Threading.Tasks;

namespace Appology.SugarSync.IO
{
    public class SugarSyncStorageRoot : SugarSyncDirectoryInfo
    {
        public SugarSyncStorageRoot(string name, SugarSyncAccount account) : base(name, account, null, null)
        {
            Name = name;
        }

        private bool _isBusy;
        
        public override bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                if (_isBusy != value)
                {
                    _isBusy = value;
                    OnPropertyChanged("IsBusy");
                }
            }
        }

        public async override Task RefreshAsync()
        {
            IsBusy = true;

            var result = await Account.GetXmlAsync(new Uri("https://api.sugarsync.com/user"));

            var rootDirectoryList = new List<SugarSyncPathInfo>();

            var workspaces = new SugarSyncDirectoryInfo("Workspaces", Account, null, new Uri(result.Element("workspaces").Value, UriKind.Absolute));
            var syncFolders = new SugarSyncDirectoryInfo("Sync Folders", Account, null, new Uri(result.Element("syncfolders").Value, UriKind.Absolute));
            var deleted = new SugarSyncDirectoryInfo("Deleted", Account, new Uri(result.Element("deleted").Value, UriKind.Absolute), null);
            var magicBriefcase = new SugarSyncDirectoryInfo("Magic Briefcase", Account, new Uri(result.Element("magicBriefcase").Value, UriKind.Absolute), null);
            var webArchive = new SugarSyncDirectoryInfo("Web Archive", Account, new Uri(result.Element("webArchive").Value, UriKind.Absolute), null);
            var mobilePhotos = new SugarSyncDirectoryInfo("Mobile Photos", Account, new Uri(result.Element("mobilePhotos").Value, UriKind.Absolute), null);
            var albums = new SugarSyncDirectoryInfo("Albums", Account, null, new Uri(result.Element("albums").Value, UriKind.Absolute));
            var recentActivities = new SugarSyncDirectoryInfo("Recent Activities", Account, null, new Uri(result.Element("recentActivities").Value, UriKind.Absolute));
            var receivedShares = new SugarSyncDirectoryInfo("Received Shares", Account, null, new Uri(result.Element("receivedShares").Value, UriKind.Absolute));
            var publicLinks = new SugarSyncDirectoryInfo("Public Links", Account, null, new Uri(result.Element("publicLinks").Value, UriKind.Absolute));

            rootDirectoryList.Add(workspaces);
            rootDirectoryList.Add(syncFolders);
            rootDirectoryList.Add(deleted);
            rootDirectoryList.Add(magicBriefcase);
            rootDirectoryList.Add(webArchive);
            rootDirectoryList.Add(mobilePhotos);
            rootDirectoryList.Add(albums);
            rootDirectoryList.Add(recentActivities);
            rootDirectoryList.Add(receivedShares);
            rootDirectoryList.Add(publicLinks);

            Contents = rootDirectoryList;

            IsBusy = false;
        }
    }
}
